function InitHealthDataList() {
	fillHealthInputData();
}

function lookupHealthData(data) {
	var i;
	for ( i = 0; i < $N.util.healthDataTypeEnum.length; i++) {
		if ($N.util.healthDataTypeEnum[i].value === data.valueType.toString()) {
			return $N.util.healthDataTypeEnum[i];
		}
	}
	return null;
}

function showHealthInputData() {
	var healthInputData = $N.ctrl.HealthInputDataCtrl.healthInputData;
	var $list = $('.health_data');
	console.log("li count = " + $(".health_data li").length);

	$(".health_data li").remove();
	if (healthInputData.length === 0) {
		var $item = $('<li></li>');
		var text = "没有数据";
		$item.html(text);
		$list.append($item);
		return;
	}

	var time = new Date();
	var i = 0;
	var timeString = "";
	for ( i = 0; i < healthInputData.length; i++) {
		var $item = $('<li></li>');
		var healthDataTypeObj = lookupHealthData(healthInputData[i]);
		if (healthDataTypeObj !== null) {
			var text = healthDataTypeObj.type + healthInputData[i].valueData + healthDataTypeObj.unit;
			$item.data('value-type', healthDataTypeObj.type);
			$item.data('value-unit', healthDataTypeObj.unit);
			$item.data('value-time', healthInputData[i].valueTime);
			$item.data('value-data', healthInputData[i].valueData);
			time.setTime(healthInputData[i].valueTime);
			timeString = time.format("yyyy-MM-dd hh:mm");
			$item.html(timeString + "  " + text);
			$list.append($item);
			$item.bind('click', function() {
				var index = $(this).index();
				var dataObj = lookupHealthData(healthInputData[index]);
				showDataChart(dataObj);
			});
		}
	}
}

function showDataChart(healthDataTypeObj) {
	$.ui.loadContent("#dataDiagram", false, false, "slide");
	var healthInputData = $N.ctrl.HealthInputDataCtrl.healthInputData;
	var valueType = healthDataTypeObj.type;

	var dataArray = new Array();
	var i = 0;
	for ( i = 0; i < healthInputData.length; i++) {
		var obj = lookupHealthData(healthInputData[i]);
		if (obj !== null && obj.type === valueType) {
			dataArray.push(healthInputData[i]);
		}
	}

	displayChart(healthDataTypeObj, dataArray);
}


function formatTime(timeStamp, formatString) {
	var time = new Date();
	var timeString = "";
	time.setTime(timeStamp);
	timeString = time.format(formatString);
	return timeString;
}

function displayChart(healthDataTypeObj, dataArray) {

	function sortByValue(a, b) {
		return parseInt(a.valueData) - parseInt(b.valueData);
	}

	function sortByTime(a, b) {
		return parseInt(a.valueTime) - parseInt(a.valueTime);
	}

	function getValues(dataArray) {
		var i;
		var values = new Array();
		for ( i = 0; i < dataArray.length; i++) {
			values.push(parseInt(dataArray[i].valueData));
		}
		return values;
	}
	
	

	dataArray.sort(sortByValue);
	var maxValue = parseInt(dataArray[dataArray.length - 1].valueData);
	var minValue = parseInt(dataArray[0].valueData);

	dataArray.sort(sortByTime);
	
	$('#container').highcharts({
		chart : {
			type : 'line'
		},
		title : {
			text : '健康数据'
		},
		subtitle : {
			text : healthDataTypeObj.type
		},
		
		xAxis : {
			allowDecimals : false,
			labels : {
				formatter : function() {
					return "";
					// clean, unformatted number for year
				}
			}
		},
		
		yAxis : {
			title : {
				text : healthDataTypeObj.type + "( " + healthDataTypeObj.unit + " )", 
			},
			labels : {
				formatter : function() {
					return this.value + healthDataTypeObj.unit;
				}
			}
		},
		tooltip : {
			formatter: function(){
				var time = formatTime(this.x, "yyyy-MM-dd hh:mm");
				return this.series.name + this.y +'<br/>' +time;
			},
			//pointFormat : '{series.name} <b>{point.y:,.0f}</b><br/>'
			
		},
		plotOptions : {
			line : {
				pointStart : parseInt(dataArray[0].valueTime),
				pointInterval : (parseInt(dataArray[dataArray.length - 1].valueTime) - parseInt(dataArray[0].valueTime)) / dataArray.length,
				marker : {
					enabled : false,
					symbol : 'circle',
					radius : 2,
					states : {
						hover : {
							enabled : true
						}
					}
				}
			}
		},
		series : [{
			name : healthDataTypeObj.type,
			//data : [null, null, null, null, null, 6, 11, 32, 110, 235, 369, 640, 1005, 1436, 2063, 3057, 4618, 6444, 9822, 15468, 20434, 24126, 27387, 29459, 31056, 31982, 32040, 31233, 29224, 27342, 26662, 26956, 27912, 28999, 28965, 27826, 25579, 25722, 24826, 24605, 24304, 23464, 23708, 24099, 24357, 24237, 24401, 24344, 23586, 22380, 21004, 17287, 14747, 13076, 12555, 12144, 11009, 10950, 10871, 10824, 10577, 10527, 10475, 10421, 10358, 10295, 10104]
			data : getValues(dataArray)
		}]
	});
};
/*
 function displayChart(healthDataTypeObj, dataArray){

 function sortByValue(a, b) {
 return parseInt(a.valueData) - parseInt(b.valueData);
 }

 function sortByTime(a, b) {
 return parseInt(a.valueTime) - parseInt(a.valueTime);
 }

 function getValues(dataArray) {
 var i;
 var values = new Array();
 for(i = 0; i < dataArray.length; i ++) {
 values.push(parseInt(dataArray[i].valueData));
 }
 return values;9
 }

 dataArray.sort(sortByValue);
 var maxValue = parseInt(dataArray[dataArray.length -1].valueData);
 var minValue = parseInt(dataArray[0].valueData);

 dataArray.sort(sortByTime);

 $('#container').highcharts({
 chart: {
 type: 'area'
 },
 title: {
 text: '健康数据',
 },
 subtitle: {
 text: healthDataTypeObj.type,
 },
 xAxis: {
 allowDecimals: true,
 labels: {
 formatter: function () {
 return this.value; // clean, unformatted number for year
 }
 }
 },
 yAxis: {
 title: {
 text: healthDataTypeObj.type + '(' + healthDataTypeObj.unit + ')',
 },
 labels: {
 formatter: function () {
 return this.value + " " + healthDataTypeObj.type + '( ' + healthDataTypeObj.unit + ' )';
 }
 }
 },
 tooltip: {
 pointFormat: '你好'
 },
 plotOptions: {
 area: {
 pointStart: parseInt(dataArray[0].valueTime),
 //pointInterval : (parseInt(dataArray[dataArray.length - 1].valueTime) - parseInt(dataArray[0].valueTime)) / dataArray.length,
 marker: {
 enabled: false,
 symbol: 'circle',
 radius: 2,
 states: {
 hover: {
 enabled: true
 }
 }
 }
 }
 },
 series: {
 data: getValues(dataArray),
 }
 });
 };
 */

function fillHealthInputData() {
	var personProfile = $N.ctrl.PersonProfileCtrl.getSelectedData(), personID = personProfile.personID;
	console.log("personID = " + personProfile.personID);
	$N.ctrl.HealthInputDataCtrl.registerClientNotify(showHealthInputData);
	$N.ctrl.HealthInputDataCtrl.getHealthInputData(personID);
}
